package com.nuo.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbcp.DataSourceConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nuo.util.PropertieUtil;

public class ConnectionManager {
	private static Logger log = LoggerFactory.getLogger(ConnectionManager.class);
	
	public static final String CONFIG_NAME = "/jdbc-config.properties";
	
	public static String DRIVER;
	public static String URL;
	public static String USERNAME;
	public static String PASSWORD;
	public static int MAXACTIVE;
	public static int MAXIDLE;
	
	private static DataSourceConnectionFactory factory;
	private static BasicDataSource bdSource;
	
	
	static{
		try {
			HashMap<String, String> map = PropertieUtil.getPropertieMap(CONFIG_NAME);
			DRIVER = map.get("jdbc.driver");
			URL = map.get("jdbc.url");
			USERNAME = map.get("jdbc.username");
			PASSWORD = map.get("jdbc.password");
			
			//数据库连接池设置
			MAXACTIVE = Integer.parseInt(map.get("dbcp.maxActive"));
			MAXIDLE = Integer.parseInt(map.get("dbcp.maxIdle"));
			
			
			Class.forName(DRIVER);
			log.info("数据库数据解析：" + "jdbc.driver:" + DRIVER
					+ " jdbc.url:" + URL 
					+ " jdbc.username:" + USERNAME
					+ " jdbc.password:" + PASSWORD
					+ " dbcp.maxActive:" + MAXACTIVE
					+ " dbcp.maxIdle:" + MAXIDLE);
		} catch (ClassNotFoundException e) {
			log.error("加载数据库驱动失败!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() throws SQLException{
		//return DriverManager.getConnection(URL, USERNAME, PASSWORD);
		return getConnectionFromPool();
	}
	
	public static Connection getConnectionFromJdbc() throws SQLException{
		return DriverManager.getConnection(URL, USERNAME, PASSWORD);
	}
	
	public static Connection getConnectionFromPool() throws SQLException{
		//bdSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(PropertieUtil.getProperties(CONFIG_NAME));
		if(factory == null){
			bdSource = new BasicDataSource();
			bdSource.setDriverClassName(DRIVER);
			bdSource.setUsername(USERNAME);
			bdSource.setPassword(PASSWORD);
			bdSource.setUrl(URL);
			
			bdSource.setMaxIdle(MAXIDLE);
			bdSource.setMaxActive(MAXACTIVE);
			factory = new DataSourceConnectionFactory(bdSource);
			
		}
        return factory.createConnection();
	}
	
	/**
	 * 打印出现在连接数的状态
	 * @param ds
	 */
    public static void printDataSourceStats() {
        System.out.println("NumActive: " + bdSource.getNumActive());
        System.out.println("NumIdle: " + bdSource.getNumIdle());
    }

    /**
     * 关闭现在的连接
     * @param ds
     * @throws SQLException
     */
    public static void shutdownDataSource() throws SQLException {
        bdSource.close();
    }
	
}
